package com.github.androiddevelopmentframework;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.github.androiddevelopmentframework.db.DBActivity;
import com.github.androiddevelopmentframework.mvp.activity.BaiduActivity;
import com.github.androiddevelopmentframework.script.activity.ScriptActivity;
import com.github.freewu.commons.dialog.DialogUtils;
import com.github.freewu.commons.http.HttpCacheFeature;
import com.github.freewu.commons.http.HttpCallBack;
import com.github.freewu.commons.http.HttpTimeOutFeature;
import com.github.freewu.commons.http.HttpUtils;
import com.github.freewu.commons.utils.DES;
import com.github.freewu.mvp.presenter.Presenter;
import com.github.freewu.mvp.view.BaseActivity;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.RequestBody;

public class MainActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @BindView(R.id.listView)
    ListView listView;

    private String[] titles = new String[]{"网络请求get", "网络请求post", "ScrollView套嵌adapter view", "webView测试"
            , "加密", "解密","显示加载view","mvp测试","文件下载","脚本化配置","数据库"};

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(View mView) {
        ButterKnife.bind(this);

        setTitle("标题");

        listView.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, titles));

        listView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        requestPermissions();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                HttpUtils.getInstance().get("MainActivity","http://www.baidu.com", new HttpCallBack() {
                    @Override
                    public void onError(Call call,Exception e,int errorCode) {

                    }

                    @Override
                    public void onSuccess(Call call, String body) {
                        DialogUtils.showDialog(getActivity(), body, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });
                    }
                }, HttpTimeOutFeature.FIFTEEN_SECOND, HttpCacheFeature.HAS_CACHE);
                break;
            case 1:
                RequestBody body = new FormBody.Builder()
                        .build();

                HttpUtils.getInstance().post("MainActivity","http://www.sina.com", body, new HttpCallBack() {
                    @Override
                    public void onError(Call call,Exception e,int errorCode) {

                    }

                    @Override
                    public void onSuccess(Call call, String body) {
                        DialogUtils.showDialog(getActivity(), body, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });
                    }
                }, HttpTimeOutFeature.FIFTEEN_SECOND, HttpCacheFeature.HAS_CACHE);
                break;
            case 2:
                startActivity(new Intent(getContext(), AdapterActivity.class));
                break;
            case 3:
                startActivity(new Intent(getContext(), WebActivity.class));
                break;
            case 4:
                byte[] bs = DES.encrypt("黄淮".getBytes());

                DialogUtils.showDialog(getActivity(), Arrays.toString(bs), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.dismissDialog();
                    }
                });
                break;
            case 5:
                byte[] bs1 = DES.encrypt("黄淮".getBytes());

                String message = new String(DES.decrypt(bs1));

                DialogUtils.showDialog(getActivity(), message, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.dismissDialog();
                    }
                });
                break;
            case 6:
                startActivity(new Intent(getContext(),LoadingActivity.class));
                break;
            case 7:
                startActivity(new Intent(getContext(), BaiduActivity.class));
                break;
            case 8:
                startActivity(new Intent(getContext(),DownLoadFileActivity.class));
                break;
            case 9:
                startActivity(new Intent(getContext(), ScriptActivity.class));
                break;
            case 10:
                startActivity(new Intent(getContext(),DBActivity.class));
                break;
        }
    }
    /**
     * 6.0请求危险权限
     */
    private void requestPermissions() {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.READ_CONTACTS,
                                Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_SMS,
                                Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE
                        }, 1);
            }
        }
    }

    /**
     * 响应回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        /**
         * 响应权限请求结果
         */
        if (requestCode == 1) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                // Permission Denied
                Toast.makeText(this, "申请权限失败", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
